package com.xdy.mdc.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.xdy.mdc.databinding.ItemImageBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: XDY.
 * @Email: 2280944114@qq.com
 * @CreateTime: 2023-08-29  03:49
 * @Description: CarouselAdapter
 * @Version: 1.0
 */
public class CarouselAdapter extends RecyclerView.Adapter<CarouselAdapter.ViewHolder> {

    private List<Integer> datum = new ArrayList<>();

    @NonNull
    @Override
    public CarouselAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemImageBinding binding = ItemImageBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull CarouselAdapter.ViewHolder holder, int position) {
        holder.bind(getData(position));
    }

    @Override
    public int getItemCount() {
        return datum.size();
    }

    public Integer getData(int position){
        return datum.get(position);
    }

    public void setDatum(List<Integer> datum){
        if (datum != null && datum.size() > 0) {
            this.datum.clear();
            this.datum.addAll(datum);
            notifyDataSetChanged();
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private final ItemImageBinding binding;

        public ViewHolder(@NonNull ItemImageBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        private void bind(Integer data){
            binding.carouselImageView.setImageResource(data);
        }
    }
}